﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sherwood.SignOn.Server.Models;
using Sherwood.SignOn.Server.Models.ViewModels;
using Sherwood.SignOn.Server.Controllers;

using System.Web;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sherwood.SignOn.Server.Models.Repositories;
using Sherwood.SignOn.Server.Models.Services;

namespace Sherwood.SignOn.Server.Tests
{
    [TestClass]
    public class VerifyTests : TestBase
    {
        /// <summary>
        /// Tests successful verification of a user account (including creation of verification code)
        /// </summary>
        [TestMethod]
        public void VerifyUserAccountTest()
        {
            //Assemble

            TestHelper.RegisterValidDummyUser("NewUser", "rvanoord@allmail.net");
            IUserAccount user = Data.UserAccounts.GetUserAccountByUsername("NewUser");
            UserAccountController accountController = new UserAccountController();
            VerifyViewModel accountViewModel = new VerifyViewModel();
            accountViewModel.UserAccountId = user.Id.ToString();
            string actualVerificationCode = user.VerificationCode;
            VerificationStatus originalVerificationStatus = user.VerificationStatus;

            //Act
            accountViewModel.VerificationCode = actualVerificationCode;
            ViewResult result = accountController.Verify(accountViewModel) as ViewResult;
            //Reload user
            user = Data.UserAccounts.GetUserAccountByUsername("NewUser");
            VerificationStatus finalVerificationStatus = user.VerificationStatus;


            //Assert
            Assert.IsTrue(originalVerificationStatus == VerificationStatus.Unverified);
            Assert.IsTrue(finalVerificationStatus == VerificationStatus.Verified); 
        }


       /// <summary>
        /// Tests verification function where verification status is "ResetPassword".
        /// In this case, nothing should happen... the verification status should remain the same.
        /// </summary>
        [TestMethod]
        public void VerifyUserAccountInWrongStatusTest()
        {
            //Assemble

            TestHelper.RegisterValidDummyUser("NewUser", "rvanoord@allmail.net");
            IUserAccount user = Data.UserAccounts.GetUserAccountByUsername("NewUser");
            UserAccountController accountController = new UserAccountController();
            VerifyViewModel accountViewModel = new VerifyViewModel();
            accountViewModel.UserAccountId = user.Id.ToString();
            string actualVerificationCode = user.VerificationCode;

            //Act
            user.SetVerificationParameters(VerificationStatus.ResetPassword, user.RequestedUrl, user.VerificationCode);
            VerificationStatus originalVerificationStatus = user.VerificationStatus;
            accountViewModel.VerificationCode = actualVerificationCode;
            ViewResult result = accountController.Verify(accountViewModel) as ViewResult;
            VerificationStatus finalVerificationStatus = user.VerificationStatus;


            //Assert
            Assert.IsTrue(originalVerificationStatus == VerificationStatus.ResetPassword);
            Assert.IsTrue(finalVerificationStatus == VerificationStatus.ResetPassword);
        }

        /// <summary>
        /// Tests verification function with an invalid verification code
        /// </summary>
        [TestMethod]
        public void VerifyUserAccountWithBadCodeTest()
        {
            //Assemble

            TestHelper.RegisterValidDummyUser("NewUser", "rvanoord@allmail.net");
            IUserAccount user = Data.UserAccounts.GetUserAccountByUsername("NewUser");
            UserAccountController accountController = new UserAccountController();
            VerifyViewModel accountViewModel = new VerifyViewModel();
            accountViewModel.UserAccountId = user.Id.ToString();
            string actualVerificationCode = user.VerificationCode;
            VerificationStatus originalVerificationStatus = user.VerificationStatus;

            //Act
            accountViewModel.VerificationCode = Guid.NewGuid().ToString();
            ViewResult result = accountController.Verify(accountViewModel) as ViewResult;
            VerificationStatus finalVerificationStatus = user.VerificationStatus;


            //Assert
            Assert.IsTrue(originalVerificationStatus == VerificationStatus.Unverified, "original verification status (" + originalVerificationStatus.ToString() + ") is not equal to 'unverified'");
            Assert.IsTrue(finalVerificationStatus == VerificationStatus.Unverified);
        }


   


    }
}
